import api from "../api/login.js";

export async function setLoginInfo(data) {
  return new Promise(async resolve => {
    wx.setStorageSync("expiresTime", data.expiresTime);
    wx.setStorageSync("accessToken", data.accessToken);
    wx.setStorageSync("userId", data.userId);
    wx.setStorageSync("refreshToken", data.refreshToken);
    wx.setStorageSync('loginInfo', JSON.stringify(data))
    try {
      // wx.showLoading()
      const res = await api.getUserInfo()
      // wx.showLoading()
      if (res.code === 0) {
        wx.setStorageSync('userInfo', JSON.stringify(res.data.user))
      }
      // const pages = getCurrentPages() //获取页面列表
      // const perpage = pages[pages.length - 1] //当前页   
      // perpage.onLoad()
      // perpage.onShow()
      // wx.hideLoading();
      // wx.hideNavigationBarLoading();
      // wx.stopPullDownRefresh();
    } catch (error) {
      console.log(error)
    }
    resolve(1)
  })
}

export async function removeLoginInfo() {
  wx.removeStorageSync('expiresTime')
  wx.removeStorageSync('accessToken')
  wx.removeStorageSync('userId')
  wx.removeStorageSync('userInfo')
  wx.removeStorageSync('refreshToken')
  wx.removeStorageSync('loginInfo')
  return true
}